package bb.lanxing.device.sgdevcie.utils

import androidx.recyclerview.widget.DiffUtil
import bb.lanxing.R
import bb.lanxing.databinding.DataBindingAdapters
import bb.lanxing.device.sgdevcie.xpair.uimodel.BleDescriptionDevice
import bb.lanxing.lib.devices.utils.BleUtils
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import kotlin.jvm.internal.Intrinsics
import kotlin.math.abs


class SearchDeviceAdapter : BaseMultiItemQuickAdapter<BleDescriptionDevice, BaseViewHolder>(
    mutableListOf()
) {
    private val devicesFound: MutableList<BleDescriptionDevice> = mutableListOf()

    var searchDeviceType: Int = -1

    private fun getRssiImgRes(i: Int): Int {
        if (i <= 25) {
            return R.drawable.ic_rssi_p100
        }
        var z = true
        if (i <= 51) {
            return R.drawable.ic_rssi_p75
        }
        if (i >= 76) {
            z = false
        }
        return if (z) R.drawable.ic_rssi_p50 else R.drawable.ic_rssi_p25
    }

    override fun convert(holder: BaseViewHolder, item: BleDescriptionDevice) {
        val itemViewType = holder.itemViewType

        if (itemViewType == TYPE_DFU_DEVICE) {
            holder.setText(R.id.tv_device_name, item.name)
        } else if (itemViewType == TYPE_NORMAL_DEVICE) {
            holder.setText(R.id.tv_device_name, item.name)
            holder.setBackgroundResource(
                R.id.tv_device_rssi, getRssiImgRes(
                    abs(item.rssi.toDouble()).toInt()
                )
            )
            holder.setText(R.id.tv_device_name, item.name)
            DataBindingAdapters.setDeviceIcon(
                holder.getView(R.id.iv_device_img), item.deviceType, ""
            )
        }
    }


    fun addDevice(bleDescriptionDevice: BleDescriptionDevice) {
        val name = bleDescriptionDevice.name ?: ""
        val deviceType = BleUtils.getDeviceType(name, bleDescriptionDevice.scanRecord)
        if (deviceType == this.searchDeviceType) {
            if (bleDescriptionDevice.itemType == TYPE_DFU_DEVICE) {
                bleDescriptionDevice.deviceType = deviceType
                devicesFound.add(0, bleDescriptionDevice)
            } else {
                val list2 = this.devicesFound
                bleDescriptionDevice.deviceType = deviceType
                list2.add(bleDescriptionDevice)
            }
            setDiffNewData(devicesFound)
        }
    }

    fun clear() {
        setNewInstance(ArrayList())
        devicesFound.clear()
    }

    companion object {
        var TYPE_DFU_DEVICE: Int = 1
        var TYPE_NORMAL_DEVICE: Int = 2
        fun defaultInstance(): SearchDeviceAdapter {
            val searchDeviceAdapter = SearchDeviceAdapter()
            searchDeviceAdapter.setDiffCallback(object :
                DiffUtil.ItemCallback<BleDescriptionDevice>() {
                override fun areItemsTheSame(
                    oldItem: BleDescriptionDevice, newItem: BleDescriptionDevice
                ): Boolean {
                    return Intrinsics.areEqual(oldItem.address, newItem.address)
                }

                override fun areContentsTheSame(
                    oldItem: BleDescriptionDevice, newItem: BleDescriptionDevice
                ): Boolean {
                    return newItem.rssi == oldItem.rssi
                }
            })
            searchDeviceAdapter.addItemType(TYPE_DFU_DEVICE, R.layout.layout_search_dfu_device_item)
            searchDeviceAdapter.addItemType(
                TYPE_NORMAL_DEVICE, R.layout.layout_search_normal_device_item
            )
            return searchDeviceAdapter
        }
    }
}
